<?php

class packing_list extends CActiveRecord {
/**
 * The followings are the available columns in table 'packing_list':
 * @var integer $id
 * @var integer $shipping_id
 * @var integer $date
 * @var string $subject
 * @var string $ref_no
 * @var string $pod
 */

/**
 * Returns the static model of the specified AR class.
 * @return CActiveRecord the static model class
 */
    public $packages = array();
    public $shipping_name = '',$pod_name='';
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'packing_list';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('shipping_id, date, subject, pod', 'required'),
            array('shipping_id', 'numerical', 'integerOnly'=>true),
            array('date','type','type'=>'date','dateFormat'=>'dd/MM/yyyy'),
            array('subject', 'length', 'max'=>125),
            array('pod', 'length', 'max'=>50),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'shipping_line'=>array(self::BELONGS_TO,'company','shipping_id'),
            'port'=>array(self::BELONGS_TO,'company','pod'),
            'packages'=>array(self::HAS_MANY,'packing_list_packages','packing_list_id'),
        	'report'=>array(self::HAS_MANY,'packing_list_report','packing_list_id'),
        );
    }
    /**
     * @return transaction save result
     */
    public function beforeSave() {
        $_date = explode("/", $this->date);
        $this->date = mktime(0,0,0,$_date[1],$_date[0],$_date[2]);
        return parent::beforeSave();
    }
    public function afterSave() {
        if(is_numeric($this-date))
            $this->date = date('d/m/Y',$this->date);
        return parent::afterSave();
    }
    public function afterFind() {
        if(is_numeric($this-date))
            $this->date = date('d/m/Y',$this->date);
        return parent::afterFind();
    }
    public function validateCargoInformations() {
        $Ecargo = array(); //Error cargo
        $Fcargo = array(); // Fine cargo
        $returnVal = '';
        if(!$this->isNewRecord) {
            foreach($this->shipment_packages() as $package) {
                foreach($package->cargo() as $c) {
                    if(count($c->packing_details())==0 || count($c->m3())==0 || count($c->qty())==0 || count($c->wt())==0)
                        if(!in_array('#cargo'.$c->id,$Ecargo))
                            $Ecargo[] = "#cargo".$c->id;
                    else
                        if(!in_array('#cargo'.$c->id,$Fcargo))
                            $Fcargo[] = '#cargo'.$c->id;
                }
            }

            $Ecargo = implode($Ecargo,',');
            $Fcargo = implode($Fcargo,',');
            $cs = Yii::app()->getClientScript();
            $s = "$('$Ecargo').button({icons:{primary:'ui-icon-circle-close'}});";
            $s .= "$('$Fcargo').button({icons:{primary:'ui-icon-circle-check'}});";
            $cs->registerScript('cargo#button',$s,CClientScript::POS_READY);
        }
    }
    public function savePackingList() {
        //Validate models
        $valid = true;
        if(!$this->validate())
            $valid = false;
        foreach($this->packages as $item) {
            if(!empty($item->marking)||!empty($item->pkg)||!empty($item->weight)||!empty($item->height)||!empty($item->width)||!empty($item->length)) {
                if(!$item->validate())
                    $valid = false;
            }
        }
        if(!$valid)
            return false;
        if(!$this->save(false))
            return false;
        foreach($this->packages as $item) {
            if(!empty($item->marking)||!empty($item->pkg)||!empty($item->weight)||!empty($item->height)||!empty($item->width)||!empty($item->length)) {
                $item->packing_list_id = $this->id;
                if(!$item->savePackages())
                    return false;
            }
        }
        return true;
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'shipping_id' => 'Shipping Line',
            'date' => 'Date',
            'ref_no' => 'Our Ref',
            'pod' => 'Port of Departure',
            'subject' => 'Subject',
        );
    }
}